package com.example.demo.controller;

import com.example.demo.model.Blog;
import com.example.demo.model.User;
import com.example.demo.service.BlogService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.HashMap;

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private BlogService blogService;

    @RequestMapping("/user")
    public Object getUser(Integer blogId, @SessionAttribute(value = "user",required = false)User user){
        if(blogId == null) {
            return user;
        }else {
            HashMap<String,Object> map = new HashMap<>();
            Blog blog = blogService.getBlogByBid(blogId);
            if(blog == null) {
                map.put("message","不存在当前blogId的文章");
                return map;
            }
            User author = userService.selectById(blog.getUserId());
            if(author == null){
                map.put("message","当前文章作者出错");
                return map;
            }
            return author;
        }
    }
}
